package com.herench.bs.transfer;

import com.herench.bs.transfer.Dialect;
import com.herench.bs.utils.DataFormatUtils;
import org.apache.cxf.endpoint.Client;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.*;

/**
 * com.herench.bs.transfer.transfer
 *
 * @author wangwenkai
 * @date 2017/10/26/10:12
 * E-mail：605064334@qq.com
 * 当日医生排班查询
 */
public class DoctorRegistSchedulingTransfer extends AbstractTransfer {

    public DoctorRegistSchedulingTransfer(String method, Client client,String messageMethod) {
        super(method, client,messageMethod);
    }
    @Override
    public String toInvokeMessage(Map<String, Object> params) {
        Document document = getTplDoc(getMethod());
        Element dataRow = document.getRootElement().element("CommitData").addElement("Data").addElement("DataRow");
        String start = (String) params.get("start");
        start = start != null ? start : "";
        dataRow.addAttribute("START_DATE", start);
        String end = (String) params.get("end");
        end = end != null ? end : "";
        dataRow.addAttribute("END_DATE", end);
//        String queryType = (String) params.get("queryType");
//        queryType = queryType != null ? queryType : "2";
//        dataRow.addAttribute("QUERY_TYPE", queryType);
//        String doctorCode = (String) params.get("doctorCode");
//        doctorCode = doctorCode != null ? doctorCode : "";
//        dataRow.addAttribute("QUERY_CONJECTION", doctorCode);
        String doctorCode = (String) params.get("doctorCode");
//        dataRow.addAttribute("QUERY_CONJECTION", doctorCode);
        if (doctorCode !=null) {
            dataRow.addAttribute("QUERY_CONJECTION", doctorCode);
            String queryType = (String) params.get("queryType");
            queryType = queryType != null ? queryType : "2";
            dataRow.addAttribute("QUERY_TYPE", queryType);
        }else{
            String deptCode = (String) params.get("deptCode");
            deptCode = deptCode != null ? deptCode : "";
            dataRow.addAttribute("QUERY_CONJECTION", deptCode);
            String queryType = (String) params.get("queryType");
            queryType = queryType != null ? queryType : "1";
            dataRow.addAttribute("QUERY_TYPE", queryType);
        }
        return document.asXML();
    }


    @Override
    public Map<String, Object> toResultMessage(String resMsg) throws Exception {
        Map<String, Object> resMap = new HashMap<>(16);
        Document document = DocumentHelper.parseText(resMsg);
        Element resultInfo = document.getRootElement().element("Result").element("Info");
        String executeStatus = resultInfo.attributeValue("EXECUTE_STATUS");
        String executeMessage = resultInfo.attributeValue("EXECUTE_MESSAGE");
        if (Dialect.getSuccessfulState().equals(executeStatus)) {
            List<Map<String, Object>> resList = new ArrayList<>();
            List<Element> dataRows = document.getRootElement().element("ReturnData").element("Data").elements();
           if (dataRows != null) {
                for (Element element : dataRows) {
                    DoctorRegistSchedulingInnerMessage innerMessage = new DoctorRegistSchedulingInnerMessage(element);
                    String schState = "0";
                    String schStateName = "正常";
                    String stopRegStatus = innerMessage.getStopRegStatus();
                    String numRemain = innerMessage.getCurrentRemainNum();
                    String numCount = innerMessage.getRegistrationLimits();
                    if (numRemain == null) {
                        numRemain = "0";
                    }
                    int num = Integer.parseInt(numRemain);
                    if (schState != "1" && num <= 0) {
                        schState = "2";
                        schStateName = "约满";
                    }

                    if ("1".equals(stopRegStatus)) {
                        schState = "1";
                        schStateName = "停诊";
                    }

                    Map<String, Object> el = new LinkedHashMap<>();
                    el.put("deptId", innerMessage.getClinicDept());
                    el.put("docId", innerMessage.getDoctorCode());
                    el.put("docName", innerMessage.getDoctor());
                    el.put("regFee", DataFormatUtils.getFormatMoney(Double.valueOf(innerMessage.getRegistFee())));
                    String schDate = DataFormatUtils.getSimpleDayString(innerMessage.clinicDate);
                    el.put("schDate", schDate);
                    el.put("schState", schState);
                    el.put("schStateName", schStateName);
                    el.put("clinicLabel", innerMessage.getClinicLabel());
                    el.put("ampm", Dialect.getAmpmCode(innerMessage.getTimeDesc()));
                    el.put("ampmName", innerMessage.getTimeDesc());
                    el.put("numCount", numCount);
                    el.put("numRemain", numRemain);
                    el.put("categorCode", Dialect.getClinicTypeCode(innerMessage.getClinicType()));
                    el.put("categorName", innerMessage.getClinicType());
                    resList.add(el);
                }
            }

            resMap.put("resDataList", resList);
            return resMap;
        } else {
            resMap.put("code", executeMessage);
            resMap.put("message", executeMessage);
            return resMap;
        }

    }

    private class DoctorRegistSchedulingInnerMessage implements InnerMessage {

        /**
         * 出诊时间
         */
        private String clinicDate;
        /**
         * 号表名称
         */
        private String clinicLabel;
        /**
         * 时段
         */
        private String timeDesc;
        /**
         * 挂号费
         */
        private String registFee;
        /**
         * 科室
         */
        private String clinicDept;
        /**
         * 医生姓名
         */
        private String doctor;
        /**
         * 号别：主任，副主任，普通
         */
        private String clinicType;
        /**
         * 当日限号数
         */
        private String registrationLimits;
        /**
         * 预约号剩余数
         */
        private String appointRemainNum;
        /**
         * 当日号剩余数
         */
        private String currentRemainNum;
        /**
         * 医生ID
         */
        private String doctorCode;
        /**
         * 当日已挂号数
         */
        private String registrationNum;
        /**
         * 自助显示类型0：不显示，其他显示
         */
        private String displayType;
        /**
         * 停诊标识 1：已停诊  其他正常
         */
        private String stopRegStatus;

        public DoctorRegistSchedulingInnerMessage(Element element) {
            this.clinicDate = element.attributeValue("CLINIC_DATE");
            this.clinicLabel = element.attributeValue("CLINIC_LABEL");
            this.timeDesc = element.attributeValue("TIME_DESC");
            this.registFee = element.attributeValue("REGIST_FEE");
            this.clinicDept = element.attributeValue("CLINIC_DEPT");
            this.doctor = element.attributeValue("DOCTOR");
            this.clinicType = element.attributeValue("CLINIC_TYPE");
            this.registrationLimits = element.attributeValue("REGISTRATION_LIMITS");
            this.appointRemainNum = element.attributeValue("APPOINT_REMAIN_NUM");
            this.currentRemainNum = element.attributeValue("CURRENT_REMAIN_NUM");
            this.doctorCode = element.attributeValue("DOCTOR_CODE");
            this.registrationNum = element.attributeValue("REGISTRATION_NUM");
            this.displayType = element.attributeValue("DISPLAY_TYPE");
            this.stopRegStatus = element.attributeValue("STOP_REG_STATUS");
        }

        public String getClinicDate() {
            return clinicDate;
        }

        public String getClinicLabel() {
            return clinicLabel;
        }

        public String getTimeDesc() {
            return timeDesc;
        }

        public String getRegistFee() {
            return registFee;
        }

        public String getClinicDept() {
            return clinicDept;
        }

        public String getDoctor() {
            return doctor;
        }

        public String getClinicType() {
            return clinicType;
        }

        public String getRegistrationLimits() {
            return registrationLimits;
        }

        public String getAppointRemainNum() {
            return appointRemainNum;
        }

        public String getCurrentRemainNum() {
            return currentRemainNum;
        }

        public String getDoctorCode() {
            return doctorCode;
        }

        public String getRegistrationNum() {
            return registrationNum;
        }

        public String getDisplayType() {
            return displayType;
        }

        public String getStopRegStatus() {
            return stopRegStatus;
        }
    }
}
